 package nc.ui.mmpps.plo.action.maintain;
 
 import java.awt.event.ActionEvent;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import nc.pubitf.mmpub.replace.RealSubsResultVO;
 import nc.pubitf.mmpub.replace.mmpps.mpm.IRealSubsQueryServiceForPPS;
 import nc.ui.ml.NCLangRes;
 import nc.ui.mmpps.plo.action.util.PoActionsUtil;
 import nc.ui.mmpps.plo.model.PloManageAppModel;
 import nc.ui.mmpps.plo.view.PloBillForm;
 import nc.ui.mmpps.replace.serviceproxy.MrpSubsClientServiceProxy;
 import nc.ui.pub.bill.BillItem;
 import nc.ui.pubapp.uif2app.DefaultExceptionHanler;
 import nc.ui.pubapp.uif2app.actions.EditAction;
 import nc.ui.pubapp.uif2app.view.ShowUpableBillForm;
 import nc.ui.uif2.IExceptionHandler;
 import nc.ui.uif2.ShowStatusBarMsgUtil;
 import nc.ui.uif2.model.AbstractAppModel;
 import nc.util.mmf.framework.base.MMNumberUtil;
 import nc.util.mmf.framework.base.MMValueCheck;
 import nc.vo.mmpps.mpm.res.MpmRes;
 import nc.vo.mmpps.mps0202.BillstatusEnum;
 import nc.vo.mmpps.mps0202.PoSourceEnum;
 import nc.vo.mmpps.mps0202.PoTypeEnum;
 import nc.vo.mmpps.mps0202.PoVO;
 import nc.vo.pub.BusinessException;
 import nc.vo.pub.lang.UFDouble;
 import nc.vo.pubapp.pattern.exception.ExceptionUtils;
 import nc.vo.uif2.LoginContext;
 
 
 public class PloEditAction
   extends EditAction
 {
   private static final long serialVersionUID = 1L;
   private String msg;
   private ShowUpableBillForm editor;
   
   public void doAction(ActionEvent e)
     throws Exception
   {
     Object[] obj = ((PloManageAppModel)getModel()).getSelectedOperaDatas();
     if (MMValueCheck.isEmpty(obj)) {
       return;
     }
     PoVO[] allPoVOs = PoActionsUtil.convertObjects2PloVOs(obj);
     
     if ((allPoVOs[0].getFbillstatus() != null) && (BillstatusEnum.OVER.equals(allPoVOs[0].getFbillstatus()))) {
       ExceptionUtils.wrappBusinessException(NCLangRes.getInstance().getStrByID("50010006_0", "050010006-0227"));
       return;
     }
     
     if (allPoVOs[0].isInherited()) {
       ExceptionUtils.wrappBusinessException(MpmRes.getPloCreateOrderUneditExpt());
     }
     
     Integer fpotype = allPoVOs[0].getFpotype();
     if ((fpotype.intValue() == PoTypeEnum.MPS.intValue()) && 
       (hasMpsSubsRelation(allPoVOs[0].getCpoid()))) {
       return;
     }
     
     if (fpotype.intValue() == PoTypeEnum.MRP.intValue()) {
       UFDouble nsubsednum = allPoVOs[0].getNsubsednum();
       
       if (hasMrpSubsRelation(nsubsednum)) {
         return;
       }
     }
     
     super.doAction(e);
     setDefaultStatus();
   }
   
 
 
	protected boolean isActionEnable() {
		Object[] selObjs = ((PloManageAppModel) getModel()).getSelectedOperaDatas();
		//add by zhoufla 20181008
		boolean unConfirm = true;
		if (selObjs != null && selObjs.length > 0) {
			PoVO[] allPoVOs = PoActionsUtil.convertObjects2PloVOs(selObjs);
			for (int i = 0; i < selObjs.length; i++) {
				PoVO poVO = allPoVOs[i];
				if (poVO.getFbillstatus() != 0) {
					unConfirm = false;
				}
			}
		}//end by zhoufla 
		return PoActionsUtil.isEDActionEnable(selObjs) && unConfirm;
	}
   
   public IExceptionHandler getExceptionHandler()
   {
     DefaultExceptionHanler handler = new DefaultExceptionHanler(getModel().getContext().getEntranceUI());
     if (exceptionHandler == null) {
       handler.setContext(getModel().getContext());
       handler.setErrormsg(getMsg());
     }
     
     return handler;
   }
   
 
   public String getMsg()
   {
     return msg;
   }
   
   public void setMsg(String msg) {
     this.msg = msg;
   }
   
 
   public ShowUpableBillForm getEditor()
   {
     return editor;
   }
   
   public void setEditor(ShowUpableBillForm editor) {
     this.editor = editor;
   }
   
 
 
   private void setDefaultStatus()
   {
     nc.ui.pubapp.bill.BillCardPanel panel = (nc.ui.pubapp.bill.BillCardPanel)getEditor().getBillCardPanel();
     PloBillForm pbf = new PloBillForm();
     pbf.disableCol(panel);
     if ((!MMValueCheck.isEmpty(panel.getHeadItem("fposource").getValueObject())) && (panel.getHeadItem("fposource").getValueObject().equals(PoSourceEnum.COMPUT)))
     {
 
       pbf.disableCol(panel.getHeadItem("cpsid"));
       pbf.disableCol(panel.getHeadItem("fplanstrategy"));
     }
     
     BillItem fposItem = getEditor().getBillCardPanel().getHeadItem("fposource");
     if (MMValueCheck.isEmpty(fposItem.getValueObject())) {
       return;
     }
     if (!fposItem.getValueObject().toString().equals(PoSourceEnum.HANDINPUT.toString())) {
       getEditor().getBillCardPanel().getHeadItem("cffileid").setEdit(false);
     }
     else
     {
       getEditor().getBillCardPanel().getHeadItem("cffileid").setEdit(true);
     }
   }
   
 
 
   private boolean hasMpsSubsRelation(String pk)
   {
     List<String> pks = new ArrayList();
     pks.add(pk);
     Map<String, List<RealSubsResultVO>> realsubsmap = null;
     IRealSubsQueryServiceForPPS service = MrpSubsClientServiceProxy.getRealSubsQueryServiceForPPS();
     try {
       realsubsmap = service.queryByRealSubsObject(pks);
     }
     catch (BusinessException e1) {
       ExceptionUtils.wrappException(e1);
     }
     if (null != realsubsmap) {
       List<RealSubsResultVO> realsubs = (List)realsubsmap.get(pk);
       if ((null != realsubs) && (!realsubs.isEmpty())) {
         ShowStatusBarMsgUtil.showErrorMsgWithClear(MpmRes.getPointOutMsg(), MpmRes.getPloEditExpt(), getModel().getContext());
         
         return true;
       }
     }
     return false;
   }
   
 
 
 
 
 
 
   private boolean hasMrpSubsRelation(UFDouble nsubsednum)
   {
     if ((MMValueCheck.isNotEmpty(nsubsednum)) && (!MMNumberUtil.isEqualZero(nsubsednum))) {
       ShowStatusBarMsgUtil.showErrorMsgWithClear(MpmRes.getPointOutMsg(), MpmRes.getPloEditExpt(), getModel().getContext());
       
       return true;
     }
     return false;
   }
 }

